package cn.iocoder.yudao.module.shop.favorite.service;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreQueryWrapper;
import cn.iocoder.yudao.framework.business.interest.service.AppInterestBaseService;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.api.shop.info.dto.ShopInfoDTO;
import cn.iocoder.yudao.module.shop.category.service.ShopCategoryService;
import cn.iocoder.yudao.module.shop.favorite.dal.dataobject.ShopFavoriteDO;
import cn.iocoder.yudao.module.shop.favorite.dal.mysql.ShopFavoriteMapper;
import cn.iocoder.yudao.module.shop.favorite.vo.AppShopFavoriteVO;
import cn.iocoder.yudao.module.shop.info.dal.dataobject.ShopInfoDO;
import cn.iocoder.yudao.module.shop.info.service.ShopInfoService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

/**
 * 店铺收藏 Service 实现类
 *
 * @author 山野羡民
 */
@Service
@Validated
public class AppShopFavoriteServiceImpl extends AppInterestBaseService<ShopInfoDTO, ShopFavoriteDO, AppShopFavoriteVO> implements AppShopFavoriteService {
    @Resource
    private ShopFavoriteMapper shopFavoriteMapper;

    @Resource
    private ShopInfoService shopInfoService;
    @Resource
    private ShopCategoryService shopCategoryService;

    @Override
    protected ShopFavoriteMapper getInjectedMapper() {
        return shopFavoriteMapper;
    }

    @Override
    public ShopInfoDTO validateSnapshotExists(Long bizId) {
        ShopInfoDO shopInfoDO = shopInfoService.validateExists(bizId);
        ShopInfoDTO respDTO = BeanUtils.toBean(shopInfoDO, ShopInfoDTO.class);
        respDTO.assignCategory(shopCategoryService.getCategory(shopInfoDO.getCategoryId()));
        return respDTO;
    }

    @Override
    public PageResult<AppShopFavoriteVO> getPage(Integer current, Integer size, Long userId) {
        CoreQueryWrapper<ShopFavoriteDO> wrapper = new CoreQueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("recycled", false);
        PageResult<ShopFavoriteDO> page = shopFavoriteMapper.selectPage(current, size, wrapper);
        if (CollUtil.isEmpty(page.getList())) {
            return PageResult.empty();
        }
        return new PageResult<>(page.getList().stream().map(this::convertToViewObject).toList(), page.getTotal());
    }

}
